package com.example.zp.liaotian;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.zp.IndexBar.widget.IndexBar;
import com.example.zp.decoration.DividerItemDecoration;
import com.example.zp.decoration.TitleItemDecoration;
import com.example.zp.sqldata.MySqliteUtlis;
import com.jbm.core.common.AccountCtrl;
import com.jbm.core.common.MainServer;
import com.jbm.core.iface.IAcross;
import com.jbm.core.iface.ICallGroup;
import com.jbm.core.iface.IRegister;
import com.jbm.onehome.iface.IAcc;
import com.jbm.onehome.iface.IAccCtrl;

import java.util.ArrayList;



/**
 * Created by donar_work on 2017/3/8.
 */

public class TestDuiHuaActivity extends Activity {
    private RecyclerView lv_Buddy = null;
    private ArrayList<Buddy_ListData> Buddy_ListData = new ArrayList<>();
    private RecyclerView.Adapter buddy_listAdapter = null;
    int local_id = 3;
    int to_id = 0;
    public static TestMsg testMsg;
    private TitleItemDecoration mDecoration;
    private LinearLayoutManager mManager;

    /**
     * 右侧边栏导航区域
     */
    private IndexBar mIndexBar;

    /**
     * 显示指示器DialogText
     */
    private TextView mTvSideBarHint;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_duihua);
        lv_Buddy = (RecyclerView) findViewById(R.id.Lv_Buddy);
        testMsg = new TestMsg(this,local_id);
        lv_Buddy.setLayoutManager(mManager = new LinearLayoutManager(this));
        Connect();

//        Buddy_ListData = MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).showdata();
////        buddy_listAdapter = new Buddy_ListAdapter(getApplication(),Buddy_ListData,local_id);
////        lv_Buddy.setAdapter(buddy_listAdapter);
//
//        lv_Buddy.setAdapter(buddy_listAdapter = new Buddy_ListAdapter(getApplication(), Buddy_ListData,local_id));
//        lv_Buddy.addItemDecoration(mDecoration = new TitleItemDecoration(this, Buddy_ListData));
//        //如果add两个，那么按照先后顺序，依次渲染。
//        //mRv.addItemDecoration(new TitleItemDecoration2(this,mDatas));
//        lv_Buddy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
//        //使用indexBar
//        mTvSideBarHint = (TextView) findViewById(R.id.tvSideBarHint);//HintTextView
//        mIndexBar = (IndexBar) findViewById(R.id.indexBar);//IndexBar
//        mIndexBar.setmPressedShowTextView(mTvSideBarHint)//设置HintTextView
//                .setNeedRealIndex(false)//是否设置需要真实的索引
//                .setmLayoutManager(mManager)//设置RecyclerView的LayoutManager
//                .setmSourceDatas(Buddy_ListData);//设置数据源

//        lv_Buddy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                View v=lv_Buddy.getChildAt(position);
//                TextView tv_ID = (TextView) v.findViewById(R.id.t_ID);
//                bundle.putInt("t_ID",t_ID);
//                intent.putExtras(bundle);
//                intent.setClass(mContext,MainActivity.class);
//                mContext.startActivity(intent);
//            }
//        });
    }
    protected void Connect(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                MainServer.g_server.Init(null, "192.168.0.36", (short) 1920, "app_mobile", testMsg);//建立连接
                IAcross rtn = MainServer.g_server.Remote(AccountCtrl.Account_Server_id,"app_server",
                        new IRegister(local_id),"Register",true,true);
//                P2PServer.g_server.Init(null,null,(short)0,"app_server",null);
//                MainServer.g_server.setM_local_id(local_id);
//                IAcross rtn = MainServer.g_server.Remote(AccountCtrl.Account_Server_id, "app_server",
//                        new IRegister(local_id), "Register", true, true);
//                IAcross accctrl = (IAcross) MainServer.g_server.Remote(AccountCtrl.Account_Server_id, "app_server",
//                        new IAccCtrl(new IAcc(local_id)), "GetAccList", true, true);//获取好友列表
                MainServer.g_server.setM_local_id(((IAcc)rtn).m_account_id);

                IAccCtrl accctrl = (IAccCtrl) MainServer.g_server.Remote(AccountCtrl.Account_Server_id, "app_server",
                        new IAccCtrl(new IAcc(local_id)), "GetAccList", true, true);
                ICallGroup icg = (ICallGroup) MainServer.g_server.CreateGroup();
                Log.e("MAIN",icg.m_info);
                if (rtn.m_code == 200) {

                    ArrayList<Object> list1 = MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).showalldata();
                    for (int i = 0; i < accctrl.m_acc.length; i++) {
//                        List ls = Arrays.asList(ac.m_acc[i].m_account_id);
//                        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//                        Map<String, Object> map = new HashMap<String, Object>();
                        if (list1.toString() == null) {
                            MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).StoringBuddydata(accctrl.m_acc[i].m_account_id);
                        } else {
                            if (list1.toString().contains(String.valueOf(accctrl.m_acc[i].m_account_id))) {
                                Log.e("重复：", "不添加！");
                            } else {
                                MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).StoringBuddydata(accctrl.m_acc[i].m_account_id);
                            }
                        }

//                        Log.e("ACshuju","m_acc:"+list.toString()+"\n"+"m_pbcontent"+accctrl.m_pbcontent.toString());
                    }
                    if (icg.m_code == 200){
                        Toast.makeText(TestDuiHuaActivity.this,"群组："+icg.m_cg_id,Toast.LENGTH_LONG).show();
                        if (list1.toString().contains(String.valueOf(icg.m_cg_id))) {
                            Log.e("重复：", "不添加！");
                        } else {
                            MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).StoringBuddydata(icg.m_cg_id);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Buddy_ListData = MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).showdata();
                            buddy_listAdapter = new Buddy_ListAdapter(getApplication(), Buddy_ListData, local_id);
                            lv_Buddy.setAdapter(buddy_listAdapter);
                            buddy_listAdapter.notifyDataSetChanged();
                            lv_Buddy.setAdapter(buddy_listAdapter = new Buddy_ListAdapter(getApplication(), Buddy_ListData, local_id));
                            lv_Buddy.addItemDecoration(mDecoration = new TitleItemDecoration(getApplication(), Buddy_ListData));
                            //如果add两个，那么按照先后顺序，依次渲染。
                            //mRv.addItemDecoration(new TitleItemDecoration2(this,mDatas));
                            lv_Buddy.addItemDecoration(new DividerItemDecoration(getApplication(), DividerItemDecoration.VERTICAL_LIST));
                            //使用indexBar
                            mTvSideBarHint = (TextView) findViewById(R.id.tvSideBarHint);//HintTextView
                            mIndexBar = (IndexBar) findViewById(R.id.indexBar);//IndexBar
                            mIndexBar.setmPressedShowTextView(mTvSideBarHint)//设置HintTextView
                                    .setNeedRealIndex(false)//是否设置需要真实的索引
                                    .setmLayoutManager(mManager)//设置RecyclerView的LayoutManager
                                    .setmSourceDatas(Buddy_ListData);//设置数据源
                        }
                    });


//                    ArrayList<Object> lis = MySqliteUtlis.GetInstan(TestDuiHuaActivity.this).showalldata();
//                    Log.e("ALLDATA:",lis.toString());//+"m_pbcontent"+accctrl.m_pbcontent.toString()
//                    Log.e("Buddy:",Buddy_ListData.get(5).getM_account_id()+"");
//                    text_duihua.setText(accctrl.toString());
                } else {
                    Log.e("Lianjie", accctrl.m_info);
                }
                System.out.println("code:" + accctrl.m_code + " info:" + accctrl.m_info + "\n");
//                if ( to_id > 0)
//                {
//                    System.out.println("local_id = "+P2PServer.g_server.getM_local_id());
//                    boolean connect = false;
//                    do
//                    {
//                        connect = AccountCtrl.g_accountctrl.Connect2Point(to_id,"app_mobile");
//                    }while (!connect);
//
//                    IAcross ars = P2PServer.g_server.SendText(to_id,"app_mobile","Hello "+to_id);
///*
//			IAcross ars = P2PServer.g_server.Remote(to_id, "app_linux",
//					new IP2P(to_id, "Test Push"), "PushPoint", true, true);
//*/
//
//                    System.out.println(ars.m_info + " " + ars.m_code);
//                }
                Looper.loop();
            }
        }).start();
    }
}
